using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using System.Web.UI.MobileControls;
using Shop.Model;
using Shop.Framework.Keys;
using Shop.Framework.Services;
using Shop.Model.Custom;

namespace Shop.Website.Controllers {
    public class ProductController : Controller {
        //
        // GET: /Product/

        public ActionResult GetProductsByCategory(int idCategory, string categoryName) {

            ProductService service = new ProductService();
            List<Product> productList = service.FindProducts(idCategory);

            List<Category> categoryList = (new CategoryService()).GetCategories();
            Category category = categoryList.FirstOrDefault(cat => cat.IdCategory == idCategory);

            ViewData[ShopKey.CurrentCategory] = category;
            ViewData[ShopKey.CurrentDesigner] = null;

            Session[SessionKey.CurrentList] = productList;

            //SEO
            ViewData[ShopKey.HeadModel] = new HeadModel {
                Description = category.Description,
                Keyword = category.Keywords,
                Tag = category.Tags,
                Title = category.Title
            };

            Session[SessionKey.CurrentContextView] = ContextView.Category;
            Session[SessionKey.CurrentHeadModel] = ViewData[ShopKey.HeadModel];

            //ViewData.Model = Session[SessionKey.CurrentList];
            PagingModel model = new PagingModel();
            DeterminePages(productList, model);
            model.CurrentPage = 1;
            Session[SessionKey.PagingModel] = model;
            ViewData.Model = GetFrom(productList, model.CurrentPage);

            return View("ProductList");
        }

        public ActionResult GetProductsByDesigner(int idDesigner, string GetProductsByDesigner) {

            ProductService service = new ProductService();
            List<Product> productList = service.FindProductsByDesigner(idDesigner);

            ViewData[ShopKey.CurrentCategory] = null;
            Designer designer = (new DesignerService()).GetDesigners().FirstOrDefault(des => des.IdDesigner == idDesigner);
            ViewData[ShopKey.CurrentDesigner] = designer;

            ////SEO
            ViewData[ShopKey.HeadModel] = new HeadModel {
                Description = designer.Description,
                Keyword = designer.Keywords,
                Tag = designer.Tags,
                Title = designer.Title
            };

            Session[SessionKey.CurrentContextView] = ContextView.Designer;
            Session[SessionKey.CurrentHeadModel] = ViewData[ShopKey.HeadModel];

            //ViewData.Model = Session[SessionKey.CurrentList];
            PagingModel model = new PagingModel();
            DeterminePages(productList, model);
            model.CurrentPage = 1;
            Session[SessionKey.PagingModel] = model;
            ViewData.Model = productList.Count == 0 ? new List<Product>() : GetFrom(productList, model.CurrentPage);

            return View("ProductList");
        }

        private static void DeterminePages(List<Product> productList, PagingModel model) {
            if (productList.Count > 0) {
                if (productList != null) {
                    model.TotalPage = productList.Count / 6;
                    if (productList.Count % 6 != 0)
                        model.TotalPage += 1;
                }

                if (productList != null) {
                    if (productList.Count > 0 && productList.Count < 6)
                        model.TotalPage = 1;
                }


            } else {
                model.CurrentPage = 0;
                model.TotalPage = 0;
            }
        }

        public ActionResult GetProductItem(int idProduct) {
            List<Product> currentList = Session[SessionKey.CurrentList] as List<Product>;
            Product product = null;

            
            if (currentList != null) {
                product = currentList.FirstOrDefault(prd => prd.IdProduct == idProduct);
            } 
            
            ProductService service = new ProductService();
            product = service.FindProduct(idProduct);
            int index = currentList.FindIndex(x => x.IdProduct == product.IdProduct);

            if(index >=0 )
              currentList[index]= product;            
                
            ViewData.Model = product;
            if (Request.IsAjaxRequest()) {
                return PartialView("~/Controls/ProductListItemControl.ascx");
            } else {
                //SEO
                ViewData[ShopKey.HeadModel] = new HeadModel {
                    Description = product.Description,
                    Keyword = product.Keywords,
                    Tag = product.Tags,
                    Title = product.Title
                };
                return View("ProductDetail");
            }
        }

        public ActionResult NavigatePage(string itemName, int page) {

            string[] splitItem = itemName.Split('-');
            if (splitItem[0] == ConstantKeys.Category) {

                //Category currentCategory = Session[SessionKey.CurrentCategory] as Category;
                Category currentCategory = null;
                if (currentCategory == null) {
                    int idCategory = Convert.ToInt32(splitItem[1]);
                    CategoryService service = new CategoryService();
                    currentCategory = service.GetCategories().FirstOrDefault(cat => cat.IdCategory == idCategory);
                }

                ViewData[ShopKey.CurrentCategory] = currentCategory;
                ViewData[ShopKey.CurrentDesigner] = null;

                //SEO
                ViewData[ShopKey.HeadModel] = new HeadModel {
                    Description = currentCategory.Description,
                    Keyword = currentCategory.Keywords,
                    Tag = currentCategory.Tags,
                    Title = currentCategory.Title
                };

                Session[SessionKey.CurrentContextView] = ContextView.Category;
                ProductService productService = new ProductService();
                var productList = productService.FindProducts(currentCategory.IdCategory);

                Session[SessionKey.CurrentList] = productList;
                PagingModel model = new PagingModel();
                DeterminePages(productList, model);
                model.CurrentPage = page;
                Session[SessionKey.PagingModel] = model;
                ViewData.Model = GetFrom(productList, model.CurrentPage);

            } else {
                //Category currentCategory = Session[SessionKey.CurrentCategory] as Category;
                Designer designer = null;
                if (designer == null) {
                    int idDesiger = Convert.ToInt32(splitItem[1]);
                    DesignerService service = new DesignerService();
                    designer = service.GetDesigners().FirstOrDefault(cat => cat.IdDesigner == idDesiger);
                }

                Session[SessionKey.CurrentContextView] = ContextView.Designer;

                ViewData[ShopKey.CurrentCategory] = null;
                ViewData[ShopKey.CurrentDesigner] = designer;

                //SEO
                ViewData[ShopKey.HeadModel] = new HeadModel {
                    Description = designer.Description,
                    Keyword = designer.Keywords,
                    Tag = designer.Tags,
                    Title = designer.Title
                };

                ProductService productService = new ProductService();
                var productList = productService.FindProductsByDesigner(designer.IdDesigner);

                Session[SessionKey.CurrentList] = productList;
                PagingModel model = new PagingModel();
                DeterminePages(productList, model);
                model.CurrentPage = page;
                Session[SessionKey.PagingModel] = model;
                ViewData.Model = GetFrom(productList, model.CurrentPage);
            }

            return View("ProductList");
        }

        private object GetFrom(List<Product> productList, int page) {

            List<Product> pdList = new List<Product>();
            //Determine start index
            int start = (page * 6) - 6;

            if (productList.Count == 0) return new List<Product>();
            if (start < 0) return null;

            while (true) {
                pdList.Add(productList[start]);
                start += 1;

                if (productList.Count == start)
                    break;

                if (pdList.Count == 6)
                    break;
            }

            return pdList;
        }        
    }
}